package com.xzj.admin.boot.controller;

import cn.hutool.core.util.ObjectUtil;
import com.xzj.admin.boot.domain.entity.SysDictType;
import com.xzj.admin.boot.service.SysDictTypeService;
import com.xzj.common.datasource.domain.PageReq;
import com.xzj.common.datasource.domain.PageResp;
import com.xzj.common.id.gen.DefaultIdHelper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;

/**
 * 字典类型表;(sys_dict_type)表控制层
 * @author : http://www.chiner.pro
 * @date : 2024-3-5
 */
@Tag(name = "字典类型表对象功能接口")
@RestController
@RequestMapping("/sysDict/type")
public class SysDictTypeController{
    @Resource
    private SysDictTypeService sysDictTypeService;

    /**
     * 通过ID查询单条数据 
     *
     * @param id 主键
     * @return 实例对象
     */
    @Operation(summary ="通过ID查询单条数据")
    @GetMapping("/queryById/{id}")
    public SysDictType queryById(@PathVariable(name = "id") long id){
        return sysDictTypeService.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param pageReq 筛选条件
     * @return 查询结果
     */
    @Operation(summary ="分页查询")
    @PostMapping("/pageQuery")
    public PageResp<SysDictType> pageQuery(@RequestBody PageReq<SysDictType> pageReq){
        return sysDictTypeService.paginQuery(pageReq);
    }

    /**
     * 新增Or更新数据
     *
     * @param sysDictType 实例对象
     * @return 实例对象
     */
    @Operation(summary ="新增Or更新数据")
    @PostMapping("/addOrUpdate")
    public SysDictType addOrUpdate(@RequestBody SysDictType sysDictType){
        if(ObjectUtil.isNotEmpty(sysDictType.getId())){
            return sysDictTypeService.update(sysDictType);
        }else{
            sysDictType.setId(DefaultIdHelper.nextIdStr());
            return sysDictTypeService.insert(sysDictType);
        }
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Operation(summary ="通过主键删除数据")
    @GetMapping("/del/{id}")
    public boolean deleteById(@PathVariable(name = "id") long id){
        return sysDictTypeService.deleteById(id);
    }

}
